/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.analystest.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.event.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.*;

/**
 * This class packs everything related to showing chart and other
 * information.
 * 
 * @author Dante Wang
 */
public class ChartPane extends JPanel {

	/** Creates new form ChartWindow */
	public ChartPane(XYSeries xySeries) {
		super();
		initComponents();

		String domainName = "Revision";
		String rangeName = "Value";

		// Init JFreeChart's chartModel
		_xySeriesCollection.addSeries(xySeries);

		generateChartModel(
			"Results", domainName, rangeName, _xySeriesCollection);

		// Init chart panel
		generateChartPanel(_chartModel);
		this.add(_chartPanel, BorderLayout.CENTER);

		// Init Information Panel
		generateInfoPanel();
		this.add(_pnlInfo, BorderLayout.EAST);

		// Refresh the ui
		this.validate();
	}

	public void addProgressListener(ChartProgressListener cpl) {
		_chartModel.addProgressListener(cpl);
	}

	public void addXYSeries(XYSeries xySeries) {
		_xySeriesCollection.addSeries(xySeries);
	}

	public void doSaveAs() throws IOException {
		_chartPanel.doSaveAs();
	}
	
	public XYPlot getXYPlot() {
		return _chartModel.getXYPlot();
	}

	public int indexOf(XYSeries xySeries) {
		return _xySeriesCollection.indexOf(xySeries);
	}

	public void removeProgressListener(ChartProgressListener cpl) {
		_chartModel.removeProgressListener(cpl);
	}

	public void removeXYSeries(XYSeries xySeries) {
		_xySeriesCollection.removeSeries(xySeries);
	}

	public void setSeriesColor(int index, Color color) {
		XYPlot plot = (XYPlot)_chartModel.getPlot();

		XYLineAndShapeRenderer render =
			(XYLineAndShapeRenderer)plot.getRenderer();

		render.setSeriesPaint(index, color);
	}

	public void setSeriesColor(XYSeries xySeries, Color color) {
		setSeriesColor(indexOf(xySeries), color);
	}

	public void showInfoText(String info) {
		_txtInfo.setText(info);
		_txtInfo.validate();
	}

	private void generateChartModel(
			String chartName, String domainName, String rangeName,
			XYDataset dataSet) {

		_chartModel =
			ChartFactory.createXYLineChart(
				chartName, domainName, rangeName, dataSet,
				PlotOrientation.VERTICAL, true, true, true);

		// Visual settings for chart canvas
		XYPlot xyPlot = (XYPlot)_chartModel.getPlot();

		xyPlot.setBackgroundPaint(Color.WHITE);
		xyPlot.setAxisOffset(new RectangleInsets(3.0, 3.0, 3.0, 3.0));
		xyPlot.setDomainGridlinePaint(Color.GRAY);
		xyPlot.setRangeGridlinePaint(Color.GRAY);
		xyPlot.setDomainCrosshairVisible(true);
		xyPlot.setRangeCrosshairVisible(true);

		// Visual settings for dots and lines of a series
		XYLineAndShapeRenderer renderer =
			(XYLineAndShapeRenderer)xyPlot.getRenderer();

		// Render shape for each data point
		renderer.setSeriesShapesVisible(0, true);
		renderer.setSeriesShapesFilled(0, true);

		// X Axis settings
		NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
		domain.setAutoTickUnitSelection(true);

		// Y Axis settings
		NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
		range.setAutoTickUnitSelection(true);
	}

	private void generateChartPanel(JFreeChart jfreeChart) {
		_chartPanel = new ChartPanel(jfreeChart);

		_chartPanel.setBackground(Color.WHITE);
		_chartPanel.setBorder(new TitledBorder("Chart"));
		_chartPanel.setMouseWheelEnabled(true);
		_chartPanel.setMouseZoomable(true);

		// Set Max and Min Height and Width to avoid ugly font after resizing 
		_chartPanel.setMaximumDrawHeight(1440);
		_chartPanel.setMaximumDrawWidth(2560);
		_chartPanel.setMinimumDrawHeight(0);
		_chartPanel.setMinimumDrawWidth(0);

		// Set a preferred size for the ChartPanel
		_chartPanel.setPreferredSize(new Dimension(1024, 768));
	}

	private void generateInfoPanel() {
		_pnlInfo = new JPanel(new BorderLayout());

		_pnlInfo.setBackground(Color.WHITE);
		_pnlInfo.setMinimumSize(new Dimension(240, 120));
		_pnlInfo.setMaximumSize(new Dimension(240, 1400));
		_pnlInfo.setPreferredSize(new Dimension(240, 360));
		_pnlInfo.setBorder(new TitledBorder("Information"));

		// Init Information Textbox
		_txtInfo = new JTextArea();

		_txtInfo.setEditable(false);
		_txtInfo.setAutoscrolls(true);
		_txtInfo.setWrapStyleWord(true);
		_txtInfo.setText("Hello World");
		_pnlInfo.add(_txtInfo, BorderLayout.NORTH);
	}

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

	private XYSeriesCollection _xySeriesCollection = new XYSeriesCollection();
	private JFreeChart _chartModel;
	private ChartPanel _chartPanel;
	private JPanel _pnlInfo;
	private JTextArea _txtInfo;

}